package com.example.project3.controller;

import com.example.project3.service.ICS.ICSServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @ClassName ICSController .java
 * @Description empty
 * @Author LiuJinyu
 * @Date 2023/6/8 16:03
 * @Version 1.0
 */

@Controller
@RequestMapping("/ics")
public class ICSController {

    private ICSServiceImpl ICSServiceImpl;

    @Autowired
    public void setICSServiceImpl(ICSServiceImpl icsServiceImpl) {
        this.ICSServiceImpl = icsServiceImpl;
    }

    @GetMapping("/get/{fileName}")
    public void getICS(@PathVariable String fileName, HttpServletResponse response) {
        try {
            response.setContentType("text/calendar;charset=UTF-8");
            OutputStream out = response.getOutputStream();
            byte[] bytes = ICSServiceImpl.getICSFile(fileName);
            out.write(bytes);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}